﻿
#include <iostream>
#include <chrono> 
#include <thread>

#include "Demo/AlgorithmIF.h"

#include "Sil/SilLabel/SilLabelIF.h"
#include "Demo/SystemTime.h"

#include "RecorderIF.h"
#include "Sil/Algorithm_RecorderIF.h"

namespace Algorithm {

uint64_t AlgorithmRunCounter = 0U;
uint32_t AlgorithmRunTime = 0U;

CalcDistance ins_CalcDistance;

// #define CONFIG_FILE 	"C:/Users/Administrator/Desktop/LogSimDemo/Simulation/config.ini"
// #define RECORDER_PORT 	200U

void algorithmInit(void)
{
	//实车环境算法内部调用初始化函数和添加信号函数
	// Mf4Recorder::init(CONFIG_FILE, SystemTime::timeStampNs());
	// std::vector<Mf4Recorder::SignalBaseInfo> recorderSignal;
	// addSignal_Algorithm_recorder(recorderSignal);
	// Mf4Recorder::addSampleSignal(RECORDER_PORT, recorderSignal);
}

void algorithmProc(const RoadMessageIF& roadMessage, const VehicleMessageIF& vehicleMessage, 
                   AlgorithmMessageIF& algorithmMessage)
{
	//获得算法运行开始时候的时间戳
	#ifdef SIM_TIMESTAMP_INJECT
	uint64_t startTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); //仿真时使用电脑系统时间戳
	#else
	uint64_t startTime = SystemTime::timeStamp(); //实车环境时使用控制器内部时间戳
	#endif


	//更新算法内需要的的时间信息
	SystemTime::updateSystemTime(); 


	//更新算法调度心跳信号
	AlgorithmRunCounter++; 


	//调度算法
	ins_CalcDistance.updateDistance(vehicleMessage.vehSpd);


	//更新算法输出消息
	algorithmMessage.timeStamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();


	//获得算法运行结束时候的时间戳
	#ifdef SIM_TIMESTAMP_INJECT
	uint64_t endTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
	#else
	uint64_t endTime = SystemTime::timeStamp(); //实车环境时使用控制器内部时间戳
	#endif

	//演示自动打标签功能
	if(AlgorithmRunCounter >= 1000U && AlgorithmRunCounter <= 1500U)
	{
		addLabel("测试自动标签1", 20, 10);
	}
	else
	{
		//do nothing
	}

	//演示自动打标签功能
	if(AlgorithmRunCounter >= 3000U && AlgorithmRunCounter <= 3300U)
	{
		addLabel("测试自动标签2", 10, 5);
	}
	else
	{
		//do nothing
	}

	//演示自动打标签功能
	if(AlgorithmRunCounter >= 10000U && AlgorithmRunCounter <= 20000U)
	{
		addLabel("测试自动标签3", 100, 100); 
	}
	else
	{
		//do nothing
	}

	AlgorithmRunTime = endTime - startTime; //算法运行时间

	//实车环境算法内部调用采样事件函数
	// Mf4Recorder::eventSample(RECORDER_PORT, SystemTime::timeStampNs());
	// Mf4Recorder::addLabel(RECORDER_PORT, getLabel(), SystemTime::timeStampNs());
	// #ifdef ENABLE_LABEL_DELETE 
	// clearLabel();
	// #endif
}

void algorithmDestroy(void)
{
	//实车环境算法内部调用数据记录销毁函数
	// Mf4Recorder::destroy();
}

} //namespace Algorithm 
